package com.limao.CacheManager;

import java.util.List;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.CrowdSmallCompanyClass;
import com.limao.DBProvider.CrowdSmallCompanyProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class CrowdSmallCompanyCacheManager extends BasicModule{

	private CrowdSmallCompanyProvider provider;
	private static CrowdSmallCompanyCacheManager instance;
	private static String key="SmallCompanyUserid";
	public static CrowdSmallCompanyCacheManager getInstance() {
		return instance;
	}

	private Cache<String, String> crowdSmallCompanyCache;
	private Cache<String, CrowdSmallCompanyClass> crowdSmallCompanyCache2;

	public CrowdSmallCompanyCacheManager() {
    	super("CrowdSmallCompanyCacheManager");
    	crowdSmallCompanyCache = CacheFactory.createCache("sys_crowd_SmallCompanyCache");
    	crowdSmallCompanyCache2 = CacheFactory.createCache("sys_crowd_SmallCompanyCache2");
		initProvider();
	}
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
	
    public void ClearObjectCache()
    {
    	crowdSmallCompanyCache.clear();
    } 
  
    public void ClearObjectCache2(String smallcompanyid)
    {
    	crowdSmallCompanyCache2.remove(smallcompanyid);
    }     
    
    public CrowdSmallCompanyClass loadSmallCompanyByid(String smallcompanyid)
    {
    	return loadSmallCompanyByid(smallcompanyid,true);
    }
    
    //flag=true 取缓存  false 不取缓存
    public CrowdSmallCompanyClass loadSmallCompanyByid(String smallcompanyid,boolean flag) 
    {
    	CrowdSmallCompanyClass tmp=null;
    	if(flag)
    	{
        	tmp= crowdSmallCompanyCache2.get(smallcompanyid);
        	if(tmp==null)
        	{
            	tmp=provider.loadSmallCompanyByid(CacheClass.getIntValue(smallcompanyid));
            	if(tmp!=null)
        		{
        			crowdSmallCompanyCache2.put(tmp.smallcompanyid,tmp);
        		}
        	}
    	}
    	else
    	{
        	tmp=provider.loadSmallCompanyByid(CacheClass.getIntValue(smallcompanyid));
        	if(tmp!=null)
    		{
    			crowdSmallCompanyCache2.put(tmp.smallcompanyid,tmp);
    		}
    	}

    	
    	return tmp;
    }
    
    
    public boolean  isSmallCompanyByUserid(int userid)
    {
    	boolean flag=false;
    	String useridStr=crowdSmallCompanyCache.get(key);
    	if(CacheClass.isEmpty(useridStr))
    	{
    		useridStr=provider.loadSmallCompanyUserid();
    		if(!CacheClass.isEmpty(useridStr))
    		{
    			if(useridStr.endsWith(","))
    			{
    				useridStr=useridStr.substring(0,useridStr.length()-1);
    			}
    			crowdSmallCompanyCache.put(key,useridStr);
    		}
    	}
    	
    	if(!CacheClass.isEmpty(useridStr))
    	{
    		String [] useridArr=useridStr.split(",");
    		for(String s:useridArr)
    		{
    			if(userid==CacheClass.getIntValue(s))
    			{
    				flag=true;
    				break;
    			}
    			
    		}
    	}
    	return flag;
    } 
	
    //取列表
    public List<CrowdSmallCompanyClass> loadSmallCompanyListBysmallcompanyid(String smallcompanyid) 
    {
    	return provider.loadSmallCompanyListBysmallcompanyid(CacheClass.getIntValue(smallcompanyid));
    }
    
    //添加
    public boolean addSmallCompany(String smallcompanyname,String toUserid,String regular_rate,int marketparentid,int pid0,int pid1,int level) 
    {
    	return provider.addSmallCompany(smallcompanyname,toUserid,regular_rate,marketparentid,pid0,pid1,level);
    }
    
    
    //修改
    public boolean editSmallCompany(String smallcompanyid,String smallcompanyname,String regular_rate) 
    {
    	return provider.editSmallCompany(smallcompanyid,smallcompanyname,regular_rate);
    } 
	//**************************************************************************************************/	
	private void initProvider() {
		if (provider == null) {
			try {
				provider = CrowdSmallCompanyProvider.getInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	


}
